import { Feature, Collection } from "ol";
import GeoJSON from 'ol/format/GeoJSON';
import * as mapUtil from '../../js/util';
import * as ftypes from '../featureTypes'
import { Modify } from "ol/interaction";
import * as tooltip from '../tooltip';
import { Draw } from "ol/interaction";
import { message } from "ant-design-vue";
import * as api from '../../../util/api';
import * as events from '../event';
import { mapManager } from "../MapManager";




/**
 * 地图上各个点和多边形的父类
 */
class MapFeature {

    static draw = null;
    static modify = null;
    static clickModifyFeatures = new Collection();
    static  oldCoordinates= null;
    static  lastCoordinates = null;
    

    constructor(f) {
        this.feature = f;
    }
    /**
     * 获取值对象
     * @returns {(import('../../../util/typedef').BaseInfo|import('../../../util/typedef').FieldInfo|import('../../../util/typedef').PartitionInfo)} 
     */
    getValueObject() {
        return this.feature.get("_valueobject");
    }

    /**
     * 设置值对象
     * @parms  {(import('../../../util/typedef').BaseInfo|import('../../../util/typedef').FieldInfo|import('../../../util/typedef').PartitionInfo)} val 
     */
    setValueOjbect(val) {
        this.feature.set("_valueobject", val);
    }

    getType() {
        return this.feature.get("_type");
    }

    setType(type) {
        this.feature.set("_type", type);
    }





    /**
     * 开始画图
     */
    static beginDraw() {

    }

    /**
     * 内部开始作画
     */
    static innerDraw(opt, onDrawEnd) {
        if (!opt.type) opt.type = "Polygon";

        if (MapFeature.draw != null) {
            mapManager.removeInteraction(MapFeature.draw)
            MapFeature.draw = null;
        }

        //添加画笔
        tooltip.showTip();
        MapFeature.draw = new Draw({
            source: mapManager.current.layers.drawSource,
            ftype: opt.ftype,
            type: opt.type,
            snapTolerance: 12,
            style: opt.style
        });
        mapManager.addInteraction(MapFeature.draw);

        //画完毕
        MapFeature.draw.on('drawend', (evt) => {
            evt.feature.set("ftype", opt.ftype);
            evt.feature.setStyle(opt.style);

            var result = true;
            if (onDrawEnd) {
                var handleResult = onDrawEnd(evt)
                if (handleResult != undefined) result = handleResult;
            }

            if (result) {
                //去掉提示
                tooltip.hideTip();

                //去掉画笔
                mapManager.current.omap.removeInteraction(MapFeature.draw);
                MapFeature.draw = null;
            } else {
                //校验失败，清空画布，重新画
                evt.feature.set("deleted",true);
                mapManager.clearDrawSource();
            }


        });

    }


    /**
     * 开始编辑
     */
    doEdit() { }

    /**
     * 取消编辑
     */
    cancel() {
        mapManager.doubleClickSelect.getFeatures().clear();
        MapFeature.cancelDraw(this.feature);
    }

    static cancelDraw(theFeature) {
        //清楚画笔
        if (MapFeature.draw != null) {
            mapManager.removeInteraction(MapFeature.draw);
            MapFeature.draw = null;
        }
        mapManager.current.layers.drawSource.clear();

        //去掉修改
        if (MapFeature.modify != null) {
            mapManager.removeInteraction(MapFeature.modify)
            MapFeature.modify = null;
        }

        //随意切换,需要清空之前的数组和还原样式等操作
        if (MapFeature.clickModifyFeatures.getLength() > 0) {
            var feature = MapFeature.clickModifyFeatures.item(0);
            MapFeature.setBorderDashed(feature, false);
            MapFeature.clickModifyFeatures.clear();
        }

        //取消之后，恢复之前的存档
        if(MapFeature.oldCoordinates != null){
            theFeature.getGeometry().setCoordinates(MapFeature.oldCoordinates);
            MapFeature.oldCoordinates = null;
        }
    }

    static setBorderDashed(feature, isDashed) {
        feature.set("lineDashed",isDashed);
    }

    /**
     * 内部方法，编辑feature
     */
    
    static editFeature(f, onEnd) {
        MapFeature.setBorderDashed(f, true);
        MapFeature.clickModifyFeatures = new Collection();
        MapFeature.clickModifyFeatures.push(f);
        MapFeature.modify = new Modify({
            features: MapFeature.clickModifyFeatures,
        });

        
        var featurelCoordinates = f.getGeometry().getCoordinates();
        MapFeature.oldCoordinates = featurelCoordinates;
        MapFeature.lastCoordinates =  featurelCoordinates;
        MapFeature.modify.on("modifystart", function (evt) {
            MapFeature.lastCoordinates = evt.features.getArray()[0].getGeometry().getCoordinates();
        });

        MapFeature.modify.on("modifyend",  (evt)=>{
            var result = onEnd(evt);
            if (!result) {
                f.getGeometry().setCoordinates(MapFeature.lastCoordinates);
            }
        });
        mapManager.addInteraction(MapFeature.modify);
    }



    /**
     * 保存成功
     */
    saveOk() {
        mapManager.doubleClickSelect.getFeatures().clear();
        //清画笔
        if (MapFeature.draw != null) {
            mapManager.removeInteraction(MapFeature.draw);
            MapFeature.draw = null;
        }
        mapManager.current.layers.drawSource.clear();

        //去掉修改
        if (MapFeature.modify != null) {
            mapManager.removeInteraction(MapFeature.modify)
            MapFeature.modify = null;
        }

        //随意切换,需要清空之前的数组和还原样式等操作
        if (MapFeature.clickModifyFeatures.getLength() > 0) {
            var feature = MapFeature.clickModifyFeatures.item(0);
            MapFeature.setBorderDashed(feature, false);
            MapFeature.clickModifyFeatures.clear();
        }
        MapFeature.oldCoordinates = null;
    }


    //get set 方法
    getBaseId() {
        return this.feature.get('base_id_');
    }
    setBaseId(baseId) {
        this.feature.set('base_id_', baseId)
    }

    getBaseName() {
        return this.feature.get('baseName');
    }

    getId() {
        return this.feature.getId();
    }
    setId(id) {
        this.feature.setId(id);
    }

    getName() {
        return this.feature.get("_name");
    }
    setName(val) {
        this.feature.set("_name", val);
    }

    //获取坐标点集合
    getCoordinateGroup() {
        var writer = new GeoJSON();
        return writer.writeGeometry(this.feature.getGeometry().clone());
    }

    /**
     * 获取面积
     */
    getArea() {
        return mapUtil.formatArea(this.feature.getGeometry());
    }

    setArea(val) {
        this.feature.set("area", val);
    }

    getStyle() {
        return this.feature.getStyle();
    }

    setStyle(s) {
        this.feature.setStyle(s);
    }

    setOpacity(s) {
        this.feature.set("opacity", s);
    }

    setViewColor(s) {
        
        this.feature.set('viewColor', s);
    }


    getViewColor() {
        return this.feature.get('viewColor');
    }

    getFeature() {
        return this.feature;
    }

    static zoomScale(zoom) {
        var scale = -1;
        if (zoom > 13) {
            switch (zoom) {
                case 14:
                    scale = 0.02;
                    break;
                case 15:
                    scale = 0.03;
                    break;
                case 16:
                    scale = 0.36;
                    break;
                case 17:
                    scale = 0.45;
                    break;
                case 18:
                    scale = 0.53;
                    break;
                case 19:
                    scale = 0.66;
                    break;
                default:
                    scale = 0.7;
                    break;
            }
        }
        return scale;
    }

    setIndex(index) {
        this.feature.set('index_', index);
    }

    /**
     * 内部方法保存
     */
    innerSave(url, data, callback) {
        message.loading({ content: '正在保存...', key: 'loading' }, 0);
        api.post(url, data, (resp) => {
            if (resp.success) { //保存成功
                message.success({ content: '保存成功！', key: 'loading' });
                MapFeature.setBorderDashed(this.feature, false);

                this.setValueOjbect(resp.object);

                //回调
                callback(resp);

                //收尾工作
                this.saveOk(); //取消画的操作 
                //通知事件保存成功
                events.fire(events.types.onSaveFeatureOk);
            } else {
                message.error({ content: '保存失败！', key: 'loading' });
            }
        });
    }
 
    static zoomed(zoom){

    }
}

export default MapFeature;